package cn.yilongquan.service.impl;

import cn.yilongquan.TbCourse;
import cn.yilongquan.mapper.TbCourseMapper;
import cn.yilongquan.service.TbCourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TbCourseServiceImpl implements TbCourseService {

    @Resource
    private TbCourseMapper courseMapper;

    @Override
    public List<TbCourse> getAllCourses() {
        return courseMapper.selectAll();
    }

    @Override
    public boolean deleteCourseById(Long id) {
        return courseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean addCourse(TbCourse course) {
        return courseMapper.insert(course) > 0;
    }

    @Override
    public boolean updateCourse(TbCourse course) {
        return courseMapper.updateById(course) > 0;
    }

    @Override
    public TbCourse getCourseById(Long id) {
        return courseMapper.selectById(id);
    }

    @Override
    public List<TbCourse> getCoursesByPage(int page, int size) {
        int start = (page - 1) * size;
        return courseMapper.selectByPage(start, size);
    }

    @Override
    public int getTotalCount() {


        return courseMapper.selectCount();
    }

    @Override
    public List<TbCourse> getCoursesByContentLike(String content) {
        return courseMapper.selectByContentLike(content);
    }

    @Override
    public int getTotalCountByContentLike(String content) {
        return courseMapper.selectCountByContentLike(content);
    }


    //批量新增课程
    @Override
    @Transactional // 事务控制：确保批量插入要么全成功，要么全失败
    public int batchAddCourses(List<TbCourse> courseList) {
        if (courseList.isEmpty()) {
            return 0;
        }

        // 批量插入（依赖MyBatis的批量插入方法）
        int total = courseMapper.batchInsert(courseList);
        return total;
    }

    @Override
    public void batchDelete(Long[] ids) {
        courseMapper.batchDelete(ids);
    }

}
